'use strict';

const db = uniCloud.database()
exports.main = async (event, context) => {
	let {relateUuid, wordBankId, studyedWords} = event;
	// 查询用户信息
	const res = await db.collection('memorization').where({
		relateUuid,
		wordBankId
	}).get();
	
	let allStudyedWords = res.data[0].studyedWords;
		
	studyedWords.map(async item => {	
		let temp = allStudyedWords.find(studyWord => studyWord.word === item.word)
		if(temp != undefined){
			if(item.statu === 1){
				if(temp.studyedNum < 7){
					await db.collection('memorization').where({
						'studyedWords.word': item.word
					}).update({
						'studyedWords.$.studyedNum': temp.studyedNum + 1,
						'studyedWords.$.forget': false,
					})
				} else {
					await db.collection('memorization').where({
						'studyedWords.word': item.word
					}).update({
						'studyedWords.$.forget': false,
					})
				}
			} else if(item.statu === 2) {
				if(temp.studyedNum < 7){
					await db.collection('memorization').where({
						'studyedWords.word': item.word
					}).update({
						'studyedWords.$.studyedNum': temp.studyedNum + 1,
						'studyedWords.$.forget': true,
					})
				}
			}


		}
	})
		
};
